/*
 * @Author: your name
 * @Date: 2021-06-07 13:51:37
 * @LastEditTime: 2021-06-18 16:28:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bazai_ebook\src\mixins\mixin.js
 */
import { mapGetters, mapActions } from 'vuex'
import { themeList, getReadTimeByMinute } from '@/utils/book'
import { saveLocation, getBookmark, getBookShelf, saveBookShelf } from '@/utils/localStorage'
import { gotoBookDetail, appendAddToShelf, computeId, removeAddFromShelf } from '@/utils/store'
import { shelf } from '@/api/store'
export const ebookMixin = {
  computed: {
    ...mapGetters('book', [
      'fileName',
      'menuVisible',
      'settingVisible',
      'defaultFontSize',
      'defaultFontFamily',
      'fontFamilyVisible',
      'defaultTheme',
      'bookAvailable',
      'progress',
      'section',
      'isPaginating',
      'currentBook',
      'navigation',
      'cover',
      'metadata',
      'paginate',
      'pagelist',
      'offsetY',
      'isBookmark'
    ]),
    themeList () {
      return themeList
    },
    getSectionName () {
      // if (this.section) {
      //   const sectionInfo = this.currentBook.section(this.section)
      //   if (sectionInfo && sectionInfo.href && this.currentBook.navigation) {
      //     const navigation = this.currentBook.navigation
      //     if (navigation.toc && navigation.toc.length) return navigation.toc[this.section].label
      //     return navigation.get(sectionInfo.href).label
      //   }
      // }
      // return '异常'
      return this.section ? this.navigation[this.section].label : ''
    }
  },
  methods: {
    ...mapActions('book', [
      'setFileName',
      'setMenuVisible',
      'setSettingVisible',
      'setDefaultFontSize',
      'setDefaultFontFamily',
      'setFontFamilyVisible',
      'setDefaultTheme',
      'setBookAvailable',
      'setProgress',
      'setSection',
      'setIsPaginating',
      'setCurrentBook',
      'setNavigation',
      'setCover',
      'setMetadata',
      'setPaginate',
      'setPagelist',
      'setOffsetY',
      'setIsBookmark'
    ]),
    // 刷新当前进度条位置
    refreshLocation () {
      // 获取电子书当前定位
      let currentLocation = null
      currentLocation = this.currentBook.rendition.currentLocation()
      if (currentLocation && currentLocation.start) {
        const startCfi = currentLocation.start.cfi
        const progress = this.currentBook.locations.percentageFromCfi(currentLocation.start.cfi)
        this.setProgress(Math.floor(progress * 100))
        this.setSection(currentLocation.start.index)
        saveLocation(this.fileName, startCfi)
        const bookmark = getBookmark(this.fileName)
        if (bookmark) {
          if (bookmark.some(item => item.cfi === startCfi)) {
            this.setIsBookmark(true)
          } else {
            this.setIsBookmark(false)
          }
        } else {
          this.setIsBookmark(false)
        }
        if (this.pagelist) {
          const totalPage = this.pagelist.length
          const currentPage = currentLocation.start.location
          if (currentPage && currentPage > 0) {
            this.setPaginate(currentPage + ' / ' + totalPage)
          } else {
            this.setPaginate('')
          }
        } else {
          this.setPaginate('')
        }
        if (this.updateProgressBg) this.updateProgressBg()
      }
    },
    display (target, cb) {
      if (target) {
        this.currentBook.rendition.display(target).then(() => {
          this.refreshLocation()
          if (cb) cb()
        })
      } else {
        this.currentBook.rendition.display().then(() => {
          this.refreshLocation()
          if (cb) cb()
        })
      }
    },
    // 隐藏菜单
    hideTitleAndMenu () {
      this.setMenuVisible(false)
      this.setSettingVisible(-1)
    },
    getReadTimeText () {
      return this.$t('book.haveRead').replace('$1', getReadTimeByMinute(this.fileName))
    }
  }
}

export const storeHomeMixin = {
  computed: {
    ...mapGetters({
      offsetY: 'book/offsetY',
      hotSearchOffsetY: 'store/hotSearchOffsetY',
      flapCardVisible: 'store/flapCardVisible'
    })
  },
  methods: {
    ...mapActions({
      setOffsetY: 'book/setOffsetY',
      setHotSearchOffsetY: 'store/setHotSearchOffsetY',
      setFlapCardVisible: 'store/setFlapCardVisible'
    }),
    showBookDetail (book) {
      this.$router.push({
        path: '/store/detail',
        query: {
          fileName: book.fileName,
          category: book.categoryText
        }
      })
    }
  }
}

export const storeShelfMixin = {
  computed: {
    ...mapGetters({
      isEditMode: 'store/isEditMode',
      shelfList: 'store/shelfList',
      shelfSelected: 'store/shelfSelected',
      shelfTitleVisible: 'store/shelfTitleVisible',
      offsetY: 'book/offsetY',
      shelfCategory: 'store/shelfCategory',
      currentType: 'store/currentType'
    })
  },
  methods: {
    ...mapActions({
      setIsEditMode: 'store/setIsEditMode',
      setShelfList: 'store/setShelfList',
      setShelfSelected: 'store/setShelfSelected',
      setShelfTitleVisible: 'store/setShelfTitleVisible',
      setOffsetY: 'book/setOffsetY',
      setShelfCategory: 'store/setShelfCategory',
      setCurrentType: 'store/setCurrentType'
    }),
    showBookDetail (book) {
      gotoBookDetail(this, book)
    },
    getCategoryList (title) {
      this.getShelfList().then(() => {
        const categoryList = this.shelfList.filter(book => book.type === 2 && book.title === title)[0]
        this.setShelfCategory(categoryList)
      })
    },
    getShelfList () {
      let shelfList = getBookShelf()
      if (!shelfList) {
        shelf().then(response => {
          if (response.status === 200 && response.data && response.data.bookList) {
            shelfList = appendAddToShelf(response.data.bookList)
            saveBookShelf(shelfList)
            return this.setShelfList(shelfList)
          }
        })
      } else {
        return this.setShelfList(shelfList)
      }
    },
    moveOutOfGroup (f) {
      this.setShelfList(this.shelfList.map(book => {
        if (book.type === 2 && book.itemList) {
          book.itemList = book.itemList.filter(subBook => !subBook.selected)
        }
        return book
      })).then(() => {
        const list = computeId(appendAddToShelf([].concat(
          removeAddFromShelf(this.shelfList), ...this.shelfSelected)))
        this.setShelfList(list).then(() => {
          this.simpleToast(this.$t('shelf.moveBookOutSuccess'))
          if (f) f()
        })
      })
    }
  }
}
